package ms_am;

import java.text.DecimalFormat;
import java.util.Date;

/**
 * @Author zkh
 * @Date 2023/2/21 10:44
 * @PackageName:ms_am
 * @ClassName: supermarket
 * @Description: 本类应用于计算水果价格
 * @Version 1.0
 */
public class SupermarketChange {
    public static void main(String[] args) {
        // 第一题 苹果8*2 = 16 + 草莓13*8=104  期望 120
        String firstPrice = calculatePrice(2,8);
        System.out.println("第一题：您本次消费"+firstPrice+"元");

        // 第二题 在第一题的基础上增加 芒果20*10 = 200 期望 320
        String secondPrice = calculatePrice(2,8,10);
        System.out.println("第二题：您本次消费"+secondPrice+"元");

        // 第三题 在第二题的基础上 草莓打8折 草莓总价减去20%为20.8 比第二题总价少20.8 期望  299.2
        String thirdPrice = calculateTotalPrice(2,8,10);
        System.out.println("第三题：您本次消费"+thirdPrice+"元");

        // 第四题 在第三题的基础上增加 满100减10的活动，所以消费超过一百则两者相差10元，期望  289.2
        String fourthPrice = calculateTotalDecrementPrice(2,8,10);
        System.out.println("第四题：您本次消费"+fourthPrice+"元");
    }

    static DecimalFormat df = new DecimalFormat("######0.00");

    /**
     * @param apples 苹果斤数
     * @param strawberries 草莓斤数
     * @return 总价
     */
    public static String calculatePrice(int apples, int strawberries)
    {
        double applePrice = createFruit(Product.Type.APPLE,apples).getTotalPrice(); // 苹果价格
        double strawberryPrice =  createFruit(Product.Type.STRAWBERRY,strawberries).getTotalPrice(); // 草莓价格
        double total = applePrice + strawberryPrice; // 计算总价
        return df.format(total);
    }

    public static String calculatePrice(int apples, int strawberries, int mangoes)
    {
        double applePrice = createFruit(Product.Type.APPLE,apples).getTotalPrice(); // 苹果价格
        double strawberryPrice = createFruit(Product.Type.STRAWBERRY,strawberries).getTotalPrice(); // 草莓价格
        double mangoPrice = createFruit(Product.Type.MANGO,mangoes).getTotalPrice(); // 芒果价格
        double total = applePrice + strawberryPrice+mangoPrice; // 计算总价
        return df.format(total);
    }

    /**
     * @param apples 苹果斤数
     * @param strawberries 草莓斤数
     * @param mangoes 芒果斤数
     * @return 总价
     */
    public static String calculateTotalPrice(int apples, int strawberries, int mangoes) {
        double applePrice = createFruit(Product.Type.APPLE,apples).getTotalPrice(); // 苹果价格
        double strawberryPrice = createFruit(Product.Type.STRAWBERRY,strawberries).getTotalPrice(); // 草莓价格
        double mangoPrice = createFruit(Product.Type.MANGO,mangoes).getTotalPrice(); // 芒果价格
        strawberryPrice = strawberryPrice*Product.DISCOUNT.getPrice(); // 草莓打折后的价格
        double totalPrice = applePrice + strawberryPrice + mangoPrice;
        return df.format(totalPrice);
    }

    /**
     * @param apples 苹果斤数
     * @param strawberries 草莓斤数
     * @param mangoes 芒果斤数
     * @return 总价
     */
    public static String calculateTotalDecrementPrice(int apples, int strawberries, int mangoes) {
        double applePrice = createFruit(Product.Type.APPLE,apples).getTotalPrice(); // 苹果价格
        double strawberryPrice = createFruit(Product.Type.STRAWBERRY,strawberries).getTotalPrice(); // 草莓价格
        double mangoPrice = createFruit(Product.Type.MANGO,mangoes).getTotalPrice(); // 芒果价格
        strawberryPrice = strawberryPrice*Product.DISCOUNT.getPrice(); // 草莓打折后的价格
        double totalPrice = applePrice + strawberryPrice + mangoPrice;
        //继续加大促销力度，购物满 100 减 10 块
        if(totalPrice>=Product.CONSUME.getPrice()){
            totalPrice-=Product.REDUCE.getPrice();
        }
        return df.format(totalPrice);
    }

    /**
     * 工厂模式处理 创作不同水果
     * @param type 水果类型
     * @param weight 水果重量
     * @return
     */
    public static Fruit createFruit(Product.Type type, int weight) {
        switch (type) {
            case APPLE:
                return new Apple(weight);
            case STRAWBERRY:
                return new Strawberry(weight);
            case MANGO:
                return new Mango(weight);
            default:
                throw new IllegalArgumentException("类型错误！");
        }
    }


    public static class Apple implements Fruit {
        private double price = Product.APPLE.getPrice(); // 苹果价格
        private double weight; // 苹果重量
        public Apple(double weight) {
            this.weight = weight;
        }
        @Override
        public double getPrice() {
            return price;
        }
        @Override
        public double getWeight() {
            return weight;
        }
        @Override
        public double getTotalPrice() {
            return weight*price;
        }
    }

    public static class Strawberry implements Fruit {
        private double price = Product.STRAWBERRIES.getPrice(); // 草莓价格
        private double weight; // 草莓重量
        public Strawberry(double weight) {
            this.weight = weight;
        }
        @Override
        public double getPrice() {
            return price;
        }
        @Override
        public double getWeight() {
            return weight;
        }
        @Override
        public double getTotalPrice() {
            return weight*price;
        }
    }

    public static class Mango implements Fruit {
        private double price = Product.MANGOES.getPrice();; // 芒果价格
        private double weight; // 芒果重量
        public Mango(double weight) {
            this.weight = weight;
        }
        @Override
        public double getPrice() {
            return price;
        }
        @Override
        public double getWeight() {
            return weight;
        }

        @Override
        public double getTotalPrice() {
            return weight*price;
        }
    }


}
